package com.zj.joke.util;

import android.annotation.SuppressLint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.zj.joke.Consts;

public class StringUtils {
	private static final String tag = StringUtils.class.getName();

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm
	 */
	@SuppressLint("SimpleDateFormat")
	public static String longDate2String5(long lngDate) {
		try {
			Date date = new Date(lngDate);
			String strFormat = Consts.DATE_TIME_FMT3;
			DateFormat format = new SimpleDateFormat(strFormat);
			return format.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return yyyy.MM.dd HH:mm
	 */
	@SuppressLint("SimpleDateFormat")
	public static String longDate2String4(long lngDate) {
		try {
			Date date = new Date(lngDate);
			String strFormat = Consts.DATE_TIME_FMT4;
			DateFormat format = new SimpleDateFormat(strFormat);
			return format.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}

	@SuppressLint("SimpleDateFormat")
	public static String longDate2String3(long lngDate) {
		try {
			Date date = new Date(lngDate);
			String strFormat = Consts.DATE_TIME_FMT;
			DateFormat format = new SimpleDateFormat(strFormat);
			return format.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}


	/**
	 * 格式化日期
	 * 
	 * @param longDate
	 * @return yyyy.MM.dd
	 */
	@SuppressLint("SimpleDateFormat")
	public static String longDate2String2(long longDate) {
		try {
			Date date = new Date(longDate);
			String strFormat = Consts.DATE_FMT2;
			DateFormat format = new SimpleDateFormat(strFormat);
			return format.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return yyyy-MM-dd
	 */
	@SuppressLint("SimpleDateFormat")
	public static String date2String1(Date date) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(Consts.DATE_FMT);
			return dateFormat.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	@SuppressLint("SimpleDateFormat")
	public static String dataTime2String(Date date) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(Consts.DATE_TIME_FMT);
			return dateFormat.format(date);
		} catch (Exception e) {
			LogUtils.Error(tag, e.getMessage(), e);
			return null;
		}
	}

	public static double scaleDouble(double db, int decimalDigits) {
		BigDecimal bd = new BigDecimal(db);
		db = bd.setScale(decimalDigits, BigDecimal.ROUND_HALF_UP).doubleValue();
		return db;
	}

	public static String convertStreamToString(InputStream inputStream,
			String charSet) throws IOException {
		if (inputStream != null) {
			StringBuilder sb = new StringBuilder();
			String line;
			try {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(inputStream, charSet));
				while ((line = reader.readLine()) != null) {
					sb.append(line).append("\n");
				}
			} finally {
				inputStream.close();
			}
			return sb.toString();
		}
		return null;
	}

	/**
	 * 将所有的数字、字母及标点全部转为全角字符，使它们与汉字同占两个字节
	 * 
	 * @param input
	 * @return
	 */
	private static String toDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}

	/**
	 * 替换、过滤特殊字符
	 * 
	 * @param str
	 * @return
	 * @throws PatternSyntaxException
	 */
	private static String strFilter(String str) throws PatternSyntaxException {
		str = str.replaceAll("【", "[").replaceAll("】", "]")
				.replaceAll("！", "!").replaceAll(" ", "");// 替换中文标号
		String regEx = "[『』]"; // 清除掉特殊字符
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	public static String formatTextViewString(String str) {
		return StringUtils.toDBC(strFilter(str));
	}

	public static String distance2String(BigDecimal distance) {
		return distance2String(distance.doubleValue());
	}

	public static String distance2String(double distance) {
		String str = "";
		DecimalFormat format = new DecimalFormat("#.##米");
		try {
			str = format.format(distance);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return str;
	}

	public static String double2String(double distance) {
		String str = "";
		DecimalFormat format = new DecimalFormat("#.##");
		try {
			str = format.format(distance);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return str;
	}
}
